package com.G3.Object;

import java.util.Random;

import com.G3.Base.MyImage;
import com.G3.Global.Assets;
import com.G3.Global.Global;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Array;

public class Character extends MyImage{

	private static Array<AtlasRegion> LIST_CHARACTER = new Array<AtlasRegion>();
	public static int PLAYER_TYPE = 0;
	private int m_type;
	protected AtlasRegion imgCharacter;
	protected TextureRegionDrawable drbLeft;
	protected TextureRegionDrawable drbRight;
	protected int velocity = 2;
	
	
	@Override
	public void loadResource() {
		super.loadResource();
		if(LIST_CHARACTER.size == 0)
		{
			LIST_CHARACTER = atlas.findRegions("character");
		}
	}
	
	public static int getSizeCharacter()
	{
		return LIST_CHARACTER.size;
	}
	
	public Character(int type, Vector2 centerPosition)
	{
		super(centerPosition, Assets.atlas.findRegion("character", 1));
		m_type = type;
		if(m_type == PLAYER_TYPE)
		{
			imgCharacter = LIST_CHARACTER.get(m_type);
		}
		else
		{
			imgCharacter = selectRamdomOpponentRegion();
		}
		drbLeft = getDrawableLeft();
		drbRight = getDrawableRight();
		this.setDrawable(drbLeft);
	}
	
	public Character(Vector2 centerVector2)
	{
		super(centerVector2, Assets.atlas.findRegion("character", 1));
		drbLeft = getDrawableLeft();
		drbRight = getDrawableRight();
		this.setDrawable(drbLeft);
	}
	
	public static int selectRandomOpponet()
	{
		if(LIST_CHARACTER.size == 0)
		{
			LIST_CHARACTER = Assets.atlas.findRegions("character");
		}
		Random r = new Random();
		int type = r.nextInt(LIST_CHARACTER.size);
		if(type == PLAYER_TYPE)
		{
			return selectRandomOpponet();
		}
		return type;
	}
	
	public AtlasRegion selectRamdomOpponentRegion()
	{
		Random r = new Random();
		int type = r.nextInt(LIST_CHARACTER.size);
		if(type == PLAYER_TYPE)
		{
			return selectRamdomOpponentRegion();
		}
		return LIST_CHARACTER.get(type);
	}
	
	
	public TextureRegionDrawable getDrawableLeft()
	{
		return new TextureRegionDrawable(imgCharacter);
	}
	
	public TextureRegionDrawable getDrawableRight()
	{
		AtlasRegion right = new AtlasRegion(imgCharacter);
		right.flip(true, false);
		return new TextureRegionDrawable(right);
	}

	@Override
	public void act(float delta) {
		 
		super.act(delta);
	}
	
	public void moveToAction(float x, float y)
	{
		if(x - this.getCenterPosition().x > 0)
		{
			if(this.getDrawable().equals(drbLeft))
			{
				this.setDrawable(drbRight);
			}
		}
		else
		{
			if(this.getDrawable().equals(drbRight))
			{
				this.setDrawable(drbLeft);
			}
		}
		float dur = getDuration(x, y);
		this.addAction(Actions.moveTo(x - this.getWidth()/2, y - this.getWidth()/2, dur));
	}
	
	public float getDuration(float x, float y)
	{
		float len = (new Vector2(x, y)).sub(this.getCenterPosition()).len();
		return len/(velocity *Global.CONVERT_TO_REAL_V);
	}
}
